<template>
  <el-container class="home_container">
<!--    头部区域-->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="logo">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">
        退出
      </el-button>
    </el-header>
<!--    页面主体区域-->
    <el-container>
<!--      侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--      下拉菜单-->
        <div class="toggle-button iconfont" :class="isCollapse ? 'icon115fangxiang_xiangzuo' : 'icon113fangxiang_xiangyou'" @click="isCollapse = !isCollapse"></div>
<!--        侧边栏菜单区 unique-opened: 只允许一个下拉项打开 collapse 是否水平折叠收起菜单 collapse-transition是否开启折叠动画-->
<!--        router 使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转-->
<!--        default-active 当前激活菜单的 index-->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff"
                 unique-opened :collapse=isCollapse :collapse-transition="false" router :default-active="activePath">
<!--          一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
<!--            一级菜单模板区域-->
            <template slot="title">
<!--              图标-->
              <i :class="iconsList[item.id]"></i>
<!--              文本-->
              <span>{{item.authName}}</span>
            </template>
<!--            二级菜单-->
            <el-menu-item :index="'/'+children.path" v-for="children in item.children" :key="children.id" @click="savaNavState(children.path)">
              <!--            二级菜单模板区域-->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <!--              文本-->
                <span>{{children.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
<!--      右边主体区域-->
      <el-main>
<!--        路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>>
  </el-container>
</template>

<style scoped>
    body {
        background-color: #ff0000;
    }
    .home_container {
      height: 100%;
    }
    .el-header {
      background-color: #373d4e;
      color: black;
      display: flex;
      /*左右贴边对齐*/
      justify-content: space-between;
      padding-left: 0;
    /*  让按钮上下不贴边对齐*/
      align-items: center;
      color: #fff;
      font-size: 20px;
    }

    .el-header div {
      display: flex;
      /*  纵向居中对齐*/
      align-items: center;
    }

    .el-header div span {
      margin-left: 15px;
    }

    .el-aside {
      background-color: #333744;
      text-align: left;
    }
    .el-aside .el-menu {
      border-right: none;
    }
    .el-aside i {
      margin-right: 5px;
    }

    .el-main {
      background-color: #eaedf1;
      color: #333;
    }
    .toggle-button {
      background-color: #4a5064;
      color: #fff;
      font-size: 10px;
      line-height: 24px;
      text-align: center;
      /*鼠标放上去变小手*/
      cursor: pointer;
    }
</style>

<script>
export default {
  data () {
    return {
    //  左侧菜单栏数据
      menuList: [],
      iconsList: {
        125: 'iconfont iconyonghuguanli',
        103: 'iconfont iconquanxianguanli',
        101: 'iconfont iconshangpinguanli',
        102: 'iconfont icondingdanguanli',
        145: 'iconfont iconshujutongji'
      },
      isCollapse: false,
      // 激活的链接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList(),
    //    将当前激活地址赋值给activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      //  清空sessionStorage
      window.sessionStorage.clear(),
      //  跳转到登录页
      this.$router.push('/login')
    },
    //  获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮，左侧菜单折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    savaNavState(activePaht) {
      window.sessionStorage.setItem('activePath', '/' + activePaht)
    }
  }
}
</script>
